package com.example.mongo.service;

import com.example.mongo.Model.bean.Chapter;
import com.example.mongo.Model.bean.Menu;
import com.example.mongo.dao.ChapterMapper;
import com.example.mongo.dao.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@CacheConfig(cacheNames = "menu")
@Service
public class MenuService {

    @Autowired
    public MenuMapper menuMapper;


    @Cacheable
    public Menu getMenuByComicId(int comicId){
        return menuMapper.getMenuByComicId(comicId);
    }

    @Cacheable
    public List<Menu> getMenusByComicType(String comicType){
        return menuMapper.getMenusByComicType(comicType);
    }

    @Cacheable
    public List<Menu> getMenusBySearch(String key){
        return menuMapper.getMenusBySearch(key);
    }

    @Cacheable
    public List<Menu> getPage(int pageNum){
        pageNum = (pageNum-1)*20;   //第一页对应0-20 第二页对应 20-40
        int limit = pageNum+20;
        return menuMapper.getPage(pageNum,limit);
    }
    //设计缺陷  应该将点击存入Redis中
    @Transactional
    @CachePut
    public Menu updateClickCount(int comicId){
        Menu menu = getMenuByComicId(comicId);
        menu.setClickCount(menu.getClickCount()+1);
        return menuMapper.save(menu);
    }


}
